package com.example.myrecyclerview_3

import android.annotation.SuppressLint
import android.app.PendingIntent.CanceledException
import android.graphics.Color
import android.os.Bundle
import android.os.Looper
import android.provider.MediaStore
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.get
import androidx.recyclerview.widget.RecyclerView
import com.example.showlocation.R
import java.io.IOException

class MediaStore_Images_query_1_Activity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_media_store_images_query2)
    }
    override fun onResume() {
        super.onResume()
        try {
            sendMessage()
        } catch (e: IOException) {
            throw RuntimeException(e)
        } catch (e: InterruptedException) {
            throw RuntimeException(e)
        }
    }
    fun sendMessage() {
        val collectionUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
        val projection = arrayOf(
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DISPLAY_NAME,
            MediaStore.Images.Media.DURATION,
            MediaStore.Images.Media.SIZE
        )
        val selection = MediaStore.Images.Media.SIZE +
                " >= ?"
        val selectionArgs = arrayOf(0.toString())
        val sortOrder = MediaStore.Images.Media.DISPLAY_NAME + " ASC"
        val contentResolver = applicationContext.contentResolver
        val cursor = contentResolver.query(
            collectionUri,
            null,
            null,
            null,
            null
        )
        Toast.makeText(applicationContext, "cursor.getCount()" + cursor!!.count, Toast.LENGTH_LONG)
            .show()
        var i = 0
        val myDataset = Array(
            cursor.count
        ) { arrayOfNulls<String>(3) }
        while (cursor.moveToNext()) {
            myDataset[i][0] = cursor.getString(32)
            myDataset[i][1] = cursor.getString(37)
            myDataset[i][2] = cursor.getString(12)
            i++
        }
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView_1)

        //recyclerView.adapter = ItemAdapter(this, myDataset)
        Thread {
            Looper.prepare()

            var myListener: RecyclerView.OnItemTouchListener = object : RecyclerView.OnItemTouchListener {
                override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {

                    // TODO("Not yet implemented")
                    Log.d("MediaStore_Images_query_1_Activity:","onInterceptTouchEvent" )
                    //return  true
                    return  false
                }

                override fun onTouchEvent(recyclerView:RecyclerView
                                          , e: MotionEvent) {
                    /*recyclerView.get(0).onTouchEvent()
                    recyclerView.get(0).findViewById<>()
                    recyclerView.get(0).addOnUnhandledKeyEventListener()
                    recyclerView.get(0).addTouchables()
                    recyclerView.get(0).callOnClick()
                    recyclerView.get(0).cancelDragAndDrop()
                    recyclerView.get(0).cancelLongPress()
                    recyclerView.get(0).cancelPendingInputEvents()
                    recyclerView.get(0).findViewWithTag<>()
                    recyclerView.get(0).findViewsWithText()
                    recyclerView.get(0).getLocationInWindow()
                    recyclerView.get(0).hasOnClickListeners()
                    recyclerView.get(0).performClick()
                    recyclerView.get(0).setBackgroundColor()
                    recyclerView.get(0).setBackgroundResource()
                    recyclerView.get(0).setOnTouchListener()
                    recyclerView.get(0).allViews
                    recyclerView.get(0).findViewTreeLifecycleOwner()*/
                    /*

        recyclerView.addOnScrollListener()
        recyclerView.stopScroll()*/
                    /*recyclerView.findFocus()
                    recyclerView.requestChildFocus()*/


                    recyclerView.scrollToPosition(5)
                    recyclerView.setBackgroundColor(Color.BLACK)
                    //recyclerView.setScrollingTouchSlop()
                    //recyclerView.get(4).allViews.contains(radioButton:RadioButton).toString()
                    //recyclerView.get(4).allViews.plus()
                    /*Log.d("MediaStore_Images_query_1_Activity:",
                      //  recyclerView.get(4).
                    )*/
                    recyclerView.get(2).setBackgroundColor(Color.CYAN)
                    Log.d("MediaStore_Images_query_1_Activity:","onTouchEvent" )
                    //TODO("Not yet implemented")
                }
                override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
                    Log.d("MediaStore_Images_query_1_Activity:","onRequestDisallowInterceptTouchEvent" )
                    //TODO("Not yet implemented")
                }
            }
            recyclerView.addOnItemTouchListener(myListener);
            Looper.loop()
        }.start()
    }
    @SuppressLint("IntentReset")
    @Throws(IOException::class, CanceledException::class)
    fun sendMessage_1(view: View?) {
        /*val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)*/
        if (view != null) {
            Log.d("sendMessage_1:", view.findViewById<TextView>(R.id.textView11).text.toString())
        }
    }
}

